package com.am.mosl.cityandroid.leftmenu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.bean.AccountBean;
import com.am.mosl.cityandroid.util.SettingUtils;
import com.am.mosl.cityandroid.widget.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import net.tsz.afinal.FinalBitmap;

import java.util.List;
import java.util.Map;

/**
 * Created by mosl on 14-6-28.
 */
public class SlidMenuAdapter extends BaseAdapter {

    private Context context;
    private List<Map<String,String>> listMapMenu;
    private FinalBitmap finalBitmap;
    private int messageNumber =-1;

    public SlidMenuAdapter(Context context,List<Map<String,String>> mapMenu){

        this.context=context;
        this.listMapMenu=mapMenu;
        finalBitmap=FinalBitmap.create(context);
    }

    public void setMessageNumber(int messageNumber) {
        this.messageNumber = messageNumber;
    }

    @Override
    public int getCount() {

        return listMapMenu.size()+1;

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(position==0){
            convertView=LayoutInflater.from(context).inflate(R.layout.slid_menu_list_item_1,parent,false);
            CircleImageView userImage=(CircleImageView)convertView.findViewById(R.id.user_image);
            TextView userName=(TextView)convertView.findViewById(R.id.user_name);
            if(SettingUtils.IsLogin(context)){
                AccountBean accountBean=SettingUtils.getUserInfo(context);
                userName.setText(accountBean.getData().getName());
                String imageUrl=accountBean.getData().getHead_img();
                if(imageUrl!=null){
                    ImageLoader.getInstance().displayImage(accountBean.getData().getHead_img(),userImage);
                }
            }
        }
        else{

            Map<String,String> mapMenu=listMapMenu.get(position-1);

            if(position==6)
            if(!SettingUtils.IsLogin(context)){
                TextView textView = new TextView(context);
                textView.setVisibility(View.GONE);
                return textView;
            }
            if(mapMenu.get("1").endsWith("消息中心")){

                convertView=LayoutInflater.from(context).inflate(R.layout.slid_menu_list_item_2,parent,false);
                ImageView itemImage=(ImageView)convertView.findViewById(R.id.sidebar_item_image);
                itemImage.setImageResource(Integer.parseInt(mapMenu.get("0")));
                TextView itemText=(TextView)convertView.findViewById(R.id.sidebar_item_text);
                itemText.setText(mapMenu.get("1"));
                TextView number = (TextView)convertView.findViewById(R.id.sidebar_item_num);
                if(messageNumber!=-1){
                    number.setVisibility(View.VISIBLE);
                    number.setText(Integer.toString(messageNumber));
                }
            }
           else{


                    convertView=LayoutInflater.from(context).inflate(R.layout.slid_menu_list_item_2,parent,false);
                    ImageView itemImage=(ImageView)convertView.findViewById(R.id.sidebar_item_image);
                    itemImage.setImageResource(Integer.parseInt(mapMenu.get("0")));
                    TextView itemText=(TextView)convertView.findViewById(R.id.sidebar_item_text);
                    itemText.setText(mapMenu.get("1"));


            }
        }
        return convertView;
    }
}
